package org.abettor.pushbox;
import java.util.ArrayList;
import java.util.List;
import org.abettor.pushbox.activity.PushBoxMainActivity;
import org.abettor.pushbox.config.Config;
import org.abettor.pushbox.db.PushBoxMapBeanManage;
import org.abettor.pushbox.model.PushBoxMapBean;
import org.abettor.pushbox.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Window;
import android.view.WindowManager;

public class MainActivity extends Activity {
	private boolean flag = false;
	Handler handler = new Handler(); 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Intent intent = new Intent();
        intent.setClass(this, PushBoxMainActivity.class);
        if(!Config.isInited(this)){//第一次运行,初始化数据
    		requestWindowFeature(Window.FEATURE_NO_TITLE);
    		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
            setContentView(R.layout.main);
            Thread thread = new Thread(){
            	@Override
            	public void run(){
            		MainActivity.this.initData();
            	}
            };
            thread.start();
            handler.postDelayed(new Runnable(){
				@Override
				public void run() { 
					if(!flag){
						handler.postDelayed(this, 2000);//没执行完,继续回执
					}else{
						startActivity(intent);
						MainActivity.this.finish();
					}
				}
            }, 2000);
        }else{//第二次运行,直接调用即可
        	startActivity(intent);
        	MainActivity.this.finish();
        }
    }
    //初始化数据
    private void initData(){
    	List<PushBoxMapBean> list1 = new ArrayList<PushBoxMapBean>();
    	for(int i = 1; i <= 80;i++){
    		PushBoxMapBean bean = new PushBoxMapBean();
    		if(i==1){
    			bean.setIssolved(2);//要解决的标志
    		}else{
    			bean.setIssolved(0);
    		}
    		bean.setType(PushBoxMapBean.CLASSIC_TYPE);
    		bean.setMissionid(i);
    		list1.add(bean);
    	}
    	//向数据库中添加记录
    	PushBoxMapBeanManage manage = new PushBoxMapBeanManage(this);
    	manage.insertPushBoxMapBean(list1);
    	//运算完成,回执flag
    	flag = true;
    	//设标志位
    	Config.saveInited(this);
    }
}